﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using classMeeting.App_Code;

namespace ClassMeeting.ClassInstructor
{
    public partial class Home : System.Web.UI.Page
    {
        protected static PagedDataSource pds = new PagedDataSource();
        protected void Page_Load(object sender, EventArgs e)
        {
            checkLogin();
            if (!IsPostBack)
                dataListBind();
        }

        protected void checkLogin()
        {
            String name = (String)Session["name"];
            String schoolId = (String)Session["schoolId"];
            CheckLogin check = new CheckLogin(name, schoolId);
            if (!check.isClassInstructor())
            {
                Response.Redirect("http://www.yiban.cn/school/index/id/5000207");
            }
            DataSet dt1 = SqlHelper.ExecuteDataset(CommandType.Text, "SELECT * FROM [classMeeting].[dbo].[admin] WHERE [schoolId] = '" + schoolId + "'");
            labUser.Text = (string)dt1.Tables[0].Rows[0]["name"];
        }

        public String getSubmitterSchoolID()
        {
            String schoolId = (String)Session["SchoolId"];
           // schoolId = "211125";
            return schoolId;
        }

        protected void dataListBind()
        {
            String name = (String)Session["name"];
           // name = "黄华龙";
            DataBind data = new DataBind();
            data.bindClaDataView(proList, pds, name, 0);
        }

        protected void proList_EditCommand(object source, DataListCommandEventArgs e)
        {
            string ID = proList.DataKeys[e.Item.ItemIndex].ToString();
            //Session["name"] = "黄华龙";
            //Session["schoolId"] = "211125";
            Response.Redirect("Declaration.Aspx?UrlID=" + System.Web.HttpContext.Current.Server.UrlEncode(ID) + "");
        }

        protected void proList_ItemDataBound(object sender, DataListItemEventArgs e)
        {
            if (e.Item.ItemType == ListItemType.Footer)
            {
                LinkButton FirstPage = e.Item.FindControl("LnkbtnFirst") as LinkButton;
                LinkButton PrePage = e.Item.FindControl("LnkbtnPre") as LinkButton;
                LinkButton NextPage = e.Item.FindControl("LnkbtnNext") as LinkButton;
                LinkButton LastPage = e.Item.FindControl("LnkbtnLast") as LinkButton;
                if (pds.IsFirstPage)
                {
                    FirstPage.Enabled = false;
                    PrePage.Enabled = false;
                }
                if (pds.IsLastPage)
                {
                    NextPage.Enabled = false;
                    LastPage.Enabled = false;
                }
            }
        }

        protected void proList_ItemCommand(object source, DataListCommandEventArgs e)
        {
            DataBind data = new DataBind();
            String name = (String)Session["name"];
            //name = "黄华龙";
            switch (e.CommandName)
            {
                case "first":
                    pds.CurrentPageIndex = 0;
                    data.bindClaDataView(proList, pds, name, pds.CurrentPageIndex);
                    break;
                case "pre":
                    pds.CurrentPageIndex = pds.CurrentPageIndex - 1;
                    data.bindClaDataView(proList, pds, name, pds.CurrentPageIndex);
                    break;
                case "next":
                    pds.CurrentPageIndex = pds.CurrentPageIndex + 1;
                    data.bindClaDataView(proList, pds, name, pds.CurrentPageIndex);
                    break;
                case "last":
                    pds.CurrentPageIndex = pds.PageCount - 1;
                    data.bindClaDataView(proList, pds, name, pds.CurrentPageIndex);
                    break;
            }
        }

        protected void libExit_Click(object sender, EventArgs e)
        {
            Session["name"] = null;
            Session["schoolId"] = null;
            Session["college"] = null;
            Response.Redirect("http://www.yiban.cn/school/index/id/5000207");
        }
    }
}